package Lee_code;

/**
 * @author blh
 * @date 2024年08月06日 16:37
 */
public class Lee_316_86 {
    public static String removeDuplicateLetters(String s) {
        int len = s.length();
        char[] chars = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        sb.append(chars[0]);
        for (int i = 1; i < chars.length; i++) {
            char aChar = chars[i];
            if (sb.length() == 0 || sb.indexOf(String.valueOf(aChar)) == -1) {
                sb.append(aChar);
            } else {
                String temp1 = sb.toString();
                String temp = sb.deleteCharAt(sb.indexOf(String.valueOf(aChar))).append(aChar).toString();
                sb = compare(temp, temp1);
            }
        }
        return sb.toString();
    }

    public static StringBuilder compare(String temp, String string) {
        int len = string.length();
        for (int i = 0; i < len; i++) {
            if (temp.charAt(i) < string.charAt(i)) {
                return new StringBuilder(temp);
            } else if (temp.charAt(i) > string.charAt(i)) {
                return new StringBuilder(string);
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(removeDuplicateLetters("bcabc"));
    }
}
